// pages/course/voiceCourse/play/play.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        courseId: null,
        isSlide: false,
        slideRate: 0,
        isPlaying: true,
        audioDuration: 0,
        audioAllTime: '00:00',
        audioTime: '00:00'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            courseId: options.courseId
        })
        this.getCourseInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function(e) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getCourseInfo: function() {
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let data = {
            online_id: this.data.courseId,
            paramter: 1
        }
        common.post('Online/online_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    courseInfo: {},
                })
            } else {
                res[0].image_info = res[0].image_info.replace(/\<img/gi, '<img style="display:block;width:100%;height:auto" ')
                that.setData({
                    courseInfo: res[0],
                    current: {
                        poster: res[0].background,
                        name: res[0].name,
                        author: res[0].teacher.teacher_name,
                        src: res[0].video
                    }
                })
                that.innerAudioContext = wx.getBackgroundAudioManager()
                that.innerAudioContext.title = res[0].name
                that.innerAudioContext.epname = res[0].name
                that.innerAudioContext.singer = res[0].teacher.teacher_name
                that.innerAudioContext.coverImgUrl = res[0].background
                that.innerAudioContext.src = res[0].video
                that.innerAudioContext.onCanplay(() => {
                    // 必须。可以当做是初始化时长
                    that.innerAudioContext.duration;
                    // 必须。不然也获取不到时长
                    setTimeout(() => {
                        let secondTime = parseInt(that.innerAudioContext.duration)
                        let minuteTime
                        let secondTimeInfo
                        let minuteTimeInfo
                        if (secondTime > 60) {
                            minuteTime = parseInt(secondTime / 60)
                            secondTime = parseInt(secondTime % 60)
                            if (secondTime > 9) {
                                secondTimeInfo = secondTime
                            } else {
                                secondTimeInfo = '0' + secondTime
                            }
                            if (minuteTime > 9) {
                                minuteTimeInfo = minuteTime
                            } else {
                                minuteTimeInfo = '0' + minuteTime
                            }
                            that.setData({
                                audioAllTime: minuteTimeInfo + ':' + secondTimeInfo
                            })
                        } else {
                            if (secondTime > 9) {
                                secondTimeInfo = secondTime
                            } else {
                                secondTimeInfo = '0' + secondTime
                            }
                            that.setData({
                                audioAllTime: '00:' + secondTimeInfo
                            })
                        }
                        that.setData({
                            audioDuration: that.innerAudioContext.duration
                        })
                    }, 500)
                })
                that.innerAudioContext.onTimeUpdate(() => {
                    that.formatSeconds(that.innerAudioContext.currentTime)
                    that.audioTimeUpdated(that.innerAudioContext.currentTime)
                })
            }
        }).catch(function(e) {})
    },

    goComment: function(e) {
        wx.navigateTo({
            url: '/pages/course/comment/comment?courseId=' + e.currentTarget.dataset.id + '&courseType=' + e.currentTarget.dataset.type
        })
    },

    audioClick: function() {
        this.setData({
            isPlaying: !this.data.isPlaying
        })
        if (this.data.isPlaying) {
            this.innerAudioContext.play();
        } else {
            this.innerAudioContext.pause();
        }
    },

    audioTimeUpdated: function(e) {
        this.setData({
            slideRate: parseInt(e / this.data.audioDuration * 10000)
        })
    },

    timeSliderChanged: function(e) {
        var time = this.data.audioDuration * e.detail.value / 10000;
        this.innerAudioContext.seek(time)
        this.formatSeconds(time)
    },

    formatSeconds: function(value) {
        const that = this
        let secondTime = parseInt(value)
        let minuteTime
        let secondTimeInfo
        let minuteTimeInfo
        if (secondTime > 60) {
            minuteTime = parseInt(secondTime / 60)
            secondTime = parseInt(secondTime % 60)
            if (secondTime > 9) {
                secondTimeInfo = secondTime
            } else {
                secondTimeInfo = '0' + secondTime
            }
            if (minuteTime > 9) {
                minuteTimeInfo = minuteTime
            } else {
                minuteTimeInfo = '0' + minuteTime
            }
            that.setData({
                audioTime: minuteTimeInfo + ':' + secondTimeInfo
            })
        } else {
            if (secondTime > 9) {
                secondTimeInfo = secondTime
            } else {
                secondTimeInfo = '0' + secondTime
            }
            that.setData({
                audioTime: '00:' + secondTimeInfo
            })
        }
    }
})